package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.ApplyMessageActivity;
import com.wnhz.work.yz.bean.ApplyMessagebean;
import com.wnhz.work.yz.view.SwitchView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/12/6.
 */
public class VisitorMessageAdapter extends BaseAdapter implements SwitchView.OnStateChangedListener {
    private Context context;
    private List<ApplyMessagebean.VisitInfoEntity.VisitDistrictInfoEntity> list = new ArrayList<>();
    private View.OnClickListener onClickListener;


    public VisitorMessageAdapter(Context context, List<ApplyMessagebean.VisitInfoEntity.VisitDistrictInfoEntity> list, View.OnClickListener onClickListener) {
        this.context = context;
        this.list = list;
        this.onClickListener = onClickListener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VisitorMessageHolder viewHolder = new VisitorMessageHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_visit_times, null);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_item_visit_name);
            viewHolder.tv_startTime = (TextView) convertView.findViewById(R.id.tv_item_startTime);
            viewHolder.tv_endTime = (TextView) convertView.findViewById(R.id.tv_item_endTime);
            viewHolder.ll_times = convertView.findViewById(R.id.ll_item_visitTimes);
            viewHolder.sw_showVisitTimes = (SwitchView) convertView.findViewById(R.id.sw_showVisitTimes);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (VisitorMessageHolder) convertView.getTag();
        }

        viewHolder.tv_name.setText(list.get(position).getVisit_district() + "");
        viewHolder.tv_startTime.setText(list.get(position).getStarttime() + "");
        viewHolder.tv_endTime.setText(list.get(position).getEndtime() + "");

        if (list.get(position).isShow()) {
            viewHolder.sw_showVisitTimes.setOpened(false);
            viewHolder.sw_showVisitTimes.setOpened(true);
            viewHolder.ll_times.setVisibility(View.VISIBLE);
        } else {
            viewHolder.sw_showVisitTimes.setOpened(true);
            viewHolder.sw_showVisitTimes.setOpened(false);
            viewHolder.ll_times.setVisibility(View.GONE);
        }
        if (ApplyMessageActivity.bean.getCanUpdata().equals("0")) {
        } else {
            viewHolder.sw_showVisitTimes.setOnStateChangedListener(this);
            viewHolder.tv_startTime.setOnClickListener(onClickListener);
            viewHolder.tv_endTime.setOnClickListener(onClickListener);

        }

        viewHolder.sw_showVisitTimes.setTag(position);
        viewHolder.tv_startTime.setTag(position);
        viewHolder.tv_endTime.setTag(position);


        return convertView;
    }

    @Override
    public void toggleToOn(View view) {
        int index = (Integer) view.getTag();
        SwitchView switchView = (SwitchView) view;
        switchView.setOpened(true);

        boolean isShow = list.get(index).isShow();
        if (isShow) {

        } else {
            list.get(index).setShow(true);
        }
        notifyDataSetChanged();

    }

    @Override
    public void toggleToOff(View view) {
        int index = (Integer) view.getTag();

        SwitchView switchView = (SwitchView) view;
        switchView.setOpened(false);

        boolean isShow = list.get(index).isShow();
        if (!isShow) {

        } else {
            list.get(index).setShow(false);
        }
        notifyDataSetChanged();
    }

    class VisitorMessageHolder {
        TextView tv_name;
        TextView tv_startTime;
        TextView tv_endTime;
        View ll_times;
        SwitchView sw_showVisitTimes;
    }
}
